<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="20" class="myLineHeight_40px">
        用户基本信息
      </el-col>
    </el-row>
    <el-form :model="detail" :rules="rules" ref="ruleForm">
      <el-row class="myMarginTop_10px" :gutter="10" v-if="type!='add'">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          用户头像 :
        </el-col>
        <el-col :span="7">
          <img class="myIcon" :src="iconSrc" v-if="detail.avatar==null || detail.avatar==''" alt="" />
          <img class="myIcon" :src="detail.avatar" v-else alt="" />
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10" v-if="type!='add'">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          用户昵称 :
        </el-col>
        <el-col :span="7">
          <el-input :readonly="is_modifly" v-model="detail.nickname" placeholder="昵称"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 用户姓名 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="user_name">
            <el-input :readonly="is_modifly" v-model="detail.user_name" placeholder="请输入用户姓名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          用户性别 :
        </el-col>
        <el-col :span="7" class="myLineHeight_40px">
          <template>
            <el-radio-group v-model="detail.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </template>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 身份证号码 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="card_no">
            <el-input :readonly="is_modifly" v-model="detail.card_no" placeholder="请输入身份证号码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          用户生日 :
        </el-col>
        <el-col :span="7">
          <el-date-picker
            :readonly="is_modifly"
            v-model="detail.birthday"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          民族 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.nation" placeholder="请输入民族" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          户口所在地 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.household_address" placeholder="请输入户口所在地" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myTitleBorderBottom">
        <el-col :span="20" class="myLineHeight_40px">
          用户联系电话及地址
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 联系电话 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="phone">
            <el-input v-model="detail.phone" placeholder="请输入联系电话" :readonly="is_modifly"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          联系地址 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.address" placeholder="请输入联系地址" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          工作单位 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.company" placeholder="请输入工作单位" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myTitleBorderBottom">
        <el-col :span="20" class="myLineHeight_40px">
          用户类型及车辆房屋信息
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 用户类型 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="residentTypeSelected">
            <el-select 
              :disabled="is_modifly"
              v-model="detail.residentTypeSelected"
              placeholder="请选择用户类型">
               <el-option
                v-for="item in Object.keys(residentTypeList)"
                :key="residentTypeList[item]"
                :label="residentTypeList[item]"
                :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 房屋信息 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="roomDisplayName">
            <el-input v-model="detail.roomDisplayName" data-toggle="modal" @focus="newHouseDialogVisible = true" placeholder="请选择房间信息" :disabled="is_modifly"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          用户车辆 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.car_desc" placeholder="用户车辆信息" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          备注 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="detail.extra_desc" placeholder="备注" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="10" :offset="3">
          <el-button v-if="type=='add'" @click="create('ruleForm')" class="myBgGreen">新 增</el-button>
          <el-button v-if="type=='update' && isShow(127)" @click="modify('ruleForm')" class="myBgGreen">保 存</el-button>
          <el-button v-if="type=='infor'" @click="goModifly($event)">编 辑</el-button>
          <el-button @click="onBack" class="myBgGreen">返 回</el-button>
        </el-col>
      </el-row>
    </el-form>
  
    <!-- 选择房屋开始 -->
    <el-dialog title="选择房屋" class="myRoleDialog" :visible.sync="newHouseDialogVisible">
      <div class="mymarginTop_10px myOV_y_400px">
        <el-row :gutter="10">
          <el-col :span="6" class="myTextAlignRight myLineHeight_40px">
            <span class="require myVA_M">*</span>
            所属小区 :
          </el-col>
          <el-col :span="17">
            <template>
              <el-select v-model="areaSelected"
                @visible-change="clickAreaSelect"
                @change="changeAreaSelect"
                :loading="areaLoading"
                placeholder="所属小区">
                <el-option
                  v-for="item in areaList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="myMarginTop_10px">
          <el-col :span="6" class="myTextAlignRight myLineHeight_40px">
            <span class="require myVA_M">*</span>
            所属楼栋 :
          </el-col>
          <el-col :span="17">
            <template>
              <el-select v-model="buildingSelected"
                @visible-change="clickBuildingSelect"
                @change="changeBuildingSelect"
                :loading="buildingLoading"
                placeholder="请选择楼栋">
                <el-option
                  v-for="item in buildingList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="myMarginTop_10px">
          <el-col :span="6" class="myTextAlignRight myLineHeight_40px">
            <span class="require myVA_M">*</span>
            所属单元 :
          </el-col>
          <el-col :span="17">
            <template>
              <el-select v-model="cellSelected"
                @visible-change="clickCellSelect"
                @change="changeCellSelect"
                :loading="cellLoading"
                placeholder="请选择单元">
                <el-option
                  v-for="item in cellList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="myMarginTop_10px">
          <el-col :span="6" class="myTextAlignRight myLineHeight_40px">
            <span class="require myVA_M">*</span>
            所属楼层 :
          </el-col>
          <el-col :span="17">
            <template>
              <el-select v-model="floorSelected"
                @visible-change="clickFloorSelect"
                @change="changeFloorSelect"
                :loading="floorLoading"
                placeholder="请选择楼层">
                <el-option
                  v-for="item in floorList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="myMarginTop_10px">
          <el-col :span="6" class="myTextAlignRight myLineHeight_40px">
            <span class="require myVA_M">*</span>
            所属房间 :
          </el-col>
          <el-col :span="17">
            <template>
              <el-select v-model="roomSelected"
                @visible-change="clickRoomSelect"
                @change="changeRoomSelect"
                :loading="roomLoading"
                placeholder="请选择房间">
                <el-option
                  v-for="item in roomList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="addRoom">提交</el-button>
        <el-button type="primary" class="myBgGreen" @click="newHouseDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>        
    <!-- 选择房屋结束 -->
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myIcon{
    width: 120px;
    height: 120px;
    border-radius: 50% !important;
  }
</style>

<script>

  import { getResidentDetail,createResident,modifyResident, residentTypeList } from '@/api/resident';
  import { getFloorList } from '@/api/floor';
  import { getCellList } from '@/api/cell';
  import { getBuildingList } from '@/api/building';
  import { getRoomList } from '@/api/room';
  import { getAreaList } from '@/api/area';
  import { r_email , r_phone } from "@/api/regular";//正则

  export default {
    data(){
      return{
        type:'',
        id: null,
        is_disabled:false,
        is_modifly:false,
        title:"",
        infor:{},
        detail:{
          
        },
        rules: {
          user_name: [
            { required: true, message: '请输入用户姓名', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          card_no:[
            {required: true, message: '请输入身份证号码', trigger: 'blur'},
            {pattern: r_email , message: '请输入正确的身份证号码', trigger: 'blur'}
          ],
          phone:[
            {required: true, message: '请输入联系电话', trigger: 'blur'},
            {pattern: r_phone , message: '请输入正确手机号码', trigger: 'blur'}
          ],
          residentTypeSelected:[
            {required: true, message: '请选择用户类型', trigger: 'change'}
          ],
          roomDisplayName:[
            {required: true, message: '请选择房间信息', trigger: 'change'}
          ]
        },
        loading: false,
    

        residentTypeSelected:null,
        residentTypeList: residentTypeList,
        loading:false,
        userTypeLoaded:false,

        areaSelected:null,
        areaName:null,
        areaLoaded: false,
        areaLoading: false,
        areaList: [],

        buildingSelected:null,
        buildingLoaded: false,
        buildingLoading: false,
        buildingList:[],

        cellSelected:null,
        cellLoaded: false,
        cellLoading: false,
        cellList:[],
        
        floorSelected:null,
        floorLoaded: false,
        floorLoading: false,
        floorList:[],

        roomSelected:null,
        roomLoaded: false,
        roomLoading: false,
        roomList:[],

        fjList:[],
        roomDisplayName:"",
        canAdd:true,
        page_size:1000,

        newHouseDialogVisible:false,
        iconSrc:"static/images/avatar1.jpg"
      }
    },
    components: {
       
    },
    created(){
      this.type = this.$route.query.type;
      this.id = this.$route.query.id;
      if(this.type=='update'){ //修改

        this.fetchDetail();
        this.is_modifly = false;
      }

      if(this.type=='add'){//新增
        this.is_modifly = false;
      }

      if(this.type=='info'){ //详情
        this.fetchDetail();
        this.is_modifly = true;
        this.is_disabled = true;
      }
    },
    methods:{
      fetchDetail() {
        let vm = this;
        this.loading = true;

        getResidentDetail(this.id)
        .then(response => {
          vm.detail = response.data;
          vm.detail.residentTypeSelected = vm.detail.resident_type + '';

          let roomInfos = vm.detail.room_infos;
          for (let i = 0; i < roomInfos.length; i++) {
            vm.detail.roomDisplayName="";
            vm.detail.roomDisplayName += roomInfos[i].area_name;
            vm.detail.roomDisplayName += roomInfos[i].display_name;
            vm.detail.roomDisplayName += ' ';
          }

          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        })
      },
      
      clickAreaSelect(val) {
        let vm = this;
        const params = {
          ril: 'S',
          page_num: 1,
          page_size: 999
        };
        if (!this.areaLoaded) {
          vm.areaLoading = true;
          getAreaList(params)
          .then(response => {
            vm.areaList = response.data.rows;
            vm.areaLoaded = true;
            vm.areaLoading = false;
          })
          .catch(error => {
            vm.areaLoading = false;
          })
        }
      },  
      changeAreaSelect() {
        this.buildingLoaded = false;
        this.buildingSelected = null;
        this.buildingList = [];
        let len = this.areaList.length;
        for(var i = 0;i<len;i++){
          if(this.areaSelected == this.areaList[i].value){
            this.areaName = this.areaList[i].label;
            
          }
        }
        
        this.changeBuildingSelect();
      },

      clickBuildingSelect(val) {
        if (!val || this.areaSelected == null) {
          return;
        }

        let vm = this;
        const params = {
          ril: 'S',
          area_id: this.areaSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.buildingLoaded) {
          getBuildingList(this.areaSelected, params)
          .then(response => {
            vm.buildingList = response.data.rows;
            vm.buildingLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeBuildingSelect() {
        this.cellLoaded = false;
        this.cellSelected = null;
        this.cellList = [];
        $(".errorLd").addClass("hide");
        $(".errorLd").closest(".col-md-6").find("input").removeClass("errorInput");
        let len = this.buildingList.length;
        for(var i = 0;i<len;i++){
          if(this.buildingSelected == this.buildingList[i].value){
            this.buildingName = this.buildingList[i].label;
          }
        }
        this.changeCellSelect();
      },
      clickCellSelect() {
        if (this.buildingSelected == null) {
          return;
        }


        let vm = this;
        const params = {
          ril: 'S',
          building_id: this.buildingSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.cellLoaded) {
          getCellList(this.areaSelected, params)
          .then(response => {
            vm.cellList = response.data.rows;
            vm.cellLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeCellSelect() {
        this.floorLoaded = false;
        this.floorSelected = null;
        this.floorList = [];
        $(".errorDy").addClass("hide");
        $(".errorDy").closest(".col-md-6").find("input").removeClass("errorInput");
        let len = this.cellList.length;
        for(var i = 0;i<len;i++){
          if(this.cellSelected == this.cellList[i].value){
            this.cellName = this.cellList[i].label;
          }
        }
        this.changeFloorSelect();
      },
      clickFloorSelect() {
        if (this.cellSelected == null) {
          return;
        }


        let vm = this;
        const params = {
          ril: 'S',
          cell_id: this.cellSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.floorLoaded) {
          getFloorList(this.areaSelected, params)
          .then(response => {
            vm.floorList = response.data.rows;
            vm.floorLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeFloorSelect() {
        this.roomLoaded = false;
        this.roomSelected = null;
        this.roomList = [];
        $(".errorLc").addClass("hide");
        $(".errorLc").closest(".col-md-6").find("input").removeClass("errorInput");
        let len = this.floorList.length;
        for(var i = 0;i<len;i++){
          if(this.floorSelected == this.floorList[i].value){
            this.floorName = this.floorList[i].label;
          }
        }
        this.changeRoomSelect();
      },
      clickRoomSelect() {
        if (this.floorSelected == null) {
          return;
        }
        let vm = this;
        const params = {
          ril: 'S',
          floor_id: this.floorSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.roomLoaded) {
          getRoomList(this.areaSelected, params)
          .then(response => {
            vm.roomList = response.data.rows;
            vm.roomLoaded = true;
          })
          .catch(error => {
          })
        }
      },
      changeRoomSelect() {
        $(".errorFj").addClass("hide");
        $(".errorFj").closest(".col-md-6").find("input").removeClass("errorInput");
        let len = this.roomList.length;
        for(var i = 0;i<len;i++){
          if(this.roomSelected == this.roomList[i].value){
            this.roomName = this.roomList[i].label;
          }
        }
        
        // console.log(JSON.stringify(this.fjList))
      },

      isError:function(){
        if((this.building_id != -1)||(this.cell_id != -1)||(this.floor_id != -1)||(this.room_id != -1)){

          $(".addROOM").attr({"disabled":true})   
        }
      },

      addRoom(){

        if (this.roomSelected == null) {
          return;
        }
        this.detail.roomDisplayName="";
        for (let i = 0; i < this.areaList.length; i++) {
          if (this.areaList[i].value === this.areaSelected) {
            this.detail.roomDisplayName += this.areaList[i].label;
          }
        }

        for (let i = 0; i < this.buildingList.length; i++) {
          if (this.buildingList[i].value === this.buildingSelected) {
            this.detail.roomDisplayName += this.buildingList[i].label;
          }
        }

        for (let i = 0; i < this.cellList.length; i++) {
          if (this.cellList[i].value === this.cellSelected) {
            this.detail.roomDisplayName += this.cellList[i].label;
          }
        }

        for (let i = 0; i < this.floorList.length; i++) {
          if (this.floorList[i].value === this.floorSelected) {
            this.detail.roomDisplayName += this.floorList[i].label;
          }
        }

        for (let i = 0; i < this.roomList.length; i++) {
          if (this.roomList[i].value === this.roomSelected) {
            this.detail.roomDisplayName += this.roomList[i].label;
          }
        }

        this.newHouseDialogVisible = false;

      },

      removeRoom(index){
        this.room_ids.splice(index, 1);
        this.fjList.splice(index, 1);
      },
   
      leaderPe:function(e){
        this.is_leader = $(e.currentTarget).find("input").val();
      },
      leaderSex:function(e){
        this.detail.sex = $(e.currentTarget).find("input").val();
        console.log(this.detail.sex)
      },
      
      modify(formName){
        this.detail.resident_type = this.detail.residentTypeSelected;
        if (this.roomSelected != null) {
          this.detail.room_ids = new Array(1);
          this.detail.room_ids[0] = this.roomSelected;
        }

        let vm = this;
        this.loading = true;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            modifyResident(this.id, this.detail)
            .then(response => {
              vm.loading = false;
              vm.$message.success("执行成功！");
              vm.onBack();
            })
            .catch(error=> {
              vm.$message.error("执行失败！"+error.response.data.error_msg);
              vm.loading = false;
            });
        } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });
      },
      create(formName){

        this.detail.room_ids = this.roomSelected;
        this.detail.resident_type = this.detail.residentTypeSelected;
        this.detail.room_ids = new Array(1);
        this.detail.room_ids[0] = this.roomSelected;

        let vm = this;
        vm.loading = true;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            createResident(this.detail)
            .then(response => {
              vm.loading = false;
              vm.$message.success("执行成功！");
              vm.onBack();
            })
            .catch(error=> {
              vm.$message.error("执行失败！"+error.response.data.error_msg);
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });
      },
      goModifly:function(){
        this.is_modifly = true;
        this.id_disabled = true;
      },
      onBack(){
        let vm=this;
        window.localStorage.setItem('pageNum',vm.$route.query.pageNum||1);
        vm.$router.go(-1);
      }

    }
  
  }


</script>



